<?php // Location of COOKIES for IE:
	  //	C:\Users\ljaljushkin.n\AppData\Roaming\Microsoft\Windows\Cookies ?>
<?php
/*MySQL Connection*/
include("DatabaseConnectionInformation.php");
$flag;
/*Cookie time to live (in days)*/
$cookieTTL = 3;
?>
<?php
/*Checking*/
function Check_Length($toCheck,$min,$max)
{
	if (strlen($toCheck) < $min or strlen($toCheck) > $max)
			return false;
	else
		return true;
}
function Check_Email()
{
	/*	|	Ìåòàñèìâîë âûáîðà
		^	Ìåòàñèìâîë íà÷àëà ñòðîêè
		\.	Ýêðàííèðîâàííàÿ òî÷êà.
		$	Ìåòàñèìâîë êîíöà ñòðîêè
		i	Âêëþ÷àåò ðåæèì case-insensitive, ò.å. áîëüøèå è ìàëåíüêèå áóêâû â âûðàæåíèè íå ðàçëè÷àþòñÿ.
		{}	Êâàíòèôèêàòîð - ýòî ñïåöèàëüíàÿ êîíñòðóêöèÿ, îïðåäåëÿþùàÿ, ñêîëüêî ðàç äîëæåí âñòðåòèòüñÿ ñèìâîë èëè ãðóïïà ñèìâîëîâ.
	*/
	if( !preg_match('|([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,6})|is',$_POST["email"]) )		
		return false;
	return true;
}
/*others checkings*/
/*................*/
function Check_Form()
{
  $flag=true;
  
  ?> <font color="red"> <center><?php
  if (! Check_Length($_POST['login'],1,20))
  {
	echo "LOGIN should be more than 1 characters and less than 20 </br>";
	$flag=false;
  }
  if (! Check_Length($_POST['nickname'],1,20))
  {
	echo "NICKNAME should be more than 1 characters and less than 20 </br>";
	$flag=false;
  }
  if (! Check_Length($_POST['pass'],3,20)) 
  {
	echo "PASSWORD should be more than 3 characters and less than 20</br>";
	$flag=false;
  }
  if (! Check_Length($_POST['email'],8,25))
  {
	echo "EMAIL should be more than 8 characters and less than 25</br>";
	$flag=false;
  }
  else if (!Check_Email())
  {
	echo "invalid characters or invalid structure of EMAIL</br>";
	$flag=false;
  }
   ?> </font> </center><?php
  return $flag;
}
?>


<html>
<head>
<title>ChitChat - Registration page</title>
</head>
<h2><center>ChitChat - Registration page</center></h2>
<center><P>The Registration Form</p></center>
<form action ="regist.php" method="POST">



<table align="center" cellspacing="2" border="1" cellpadding="5">
<tr>
  <td>
		Login: 		
  </td>
  <td>
		<input type="text" name="login" 
			value="<?php
				if(!empty($_POST["login"])) 
					echo htmlspecialchars(stripslashes($_POST["login"]));
				else
					echo "";
			?>">
		
  </td>
</tr>
<tr>
	<td>
		Password: 	
	</td>
	<td>
		<input type="password" name="pass"> 
  </td>
</tr>
<tr>
	<td>
		Email: 	
	</td>
	<td>
		<input type="text" name="email" 
			value="<?php 
				if(!empty($_POST["email"])) 
					echo htmlspecialchars(stripslashes($_POST["email"]));
				else
					echo "";
			?>">
	</td>
</tr>
<tr>
	<td>
		Nickname:
	</td>
	<td>
		<input type="text" name="nickname"
			value="<?php 
				if(!empty($_POST["nickname"])) 
					echo htmlspecialchars(stripslashes($_POST["nickname"]));
				else
					echo "";
			?>"> 
	</td>
</tr>
<tr>
	<td colspan=2 align=center>
		<input type="submit" value="SIGN UP:"> <br>
	</td>
</tr>
</form>

<?php
	if ( (isset($_POST["login"])) && ( isset($_POST["pass"])) && ( isset($_POST["email"])) && ( isset($_POST["nickname"])) )
	{
		if (Check_Form())
		{
			
			$dbh = mysql_connect($host, $user, $pswd) or die("Cannot connect to MySQL.");
			mysql_select_db($database) or die("Cannot connect to database.");

			$l=$_POST['login'];
			$p=$_POST['pass'];
			$n=$_POST['nickname'];
			$e=$_POST['email'];

			$l = htmlspecialchars(stripslashes($l));
			$p = htmlspecialchars(stripslashes($p));
			$n = htmlspecialchars(stripslashes($n));
			$e = htmlspecialchars(stripslashes($e));			
			$query="INSERT INTO `$database`.`user` VALUES ('$l','$p','$n','$e');";
			
			/*var_dump($query);*/
			$res = mysql_query($query);
			//var_dump($res);
			if ($res)
			{
				?>
					<center> <font color="green">  
						SUCCESSFUL REGISTRATION! </br>
					</font>
					<font color="blue">  
						your LOGIN: <?php echo htmlspecialchars(stripslashes($_POST['login']));?></br>
						your NICK:  <?php echo htmlspecialchars(stripslashes($_POST['nickname']));?> </br>
					</font>
					</center> 
				<?php
			}
			else
			{
				//$row = mysql_fetch_array($res);
				//var_dump($row);
				?>
					<font color="red"> <center> LOGIN  '
					<?php
						echo htmlspecialchars(stripslashes($_POST['login']));
					?>
					'  busy!</center></font> 
					<h3><center>
						<font color="red"> Fill the field(s) again! </font>
					</center></h3>
				<?php
			}

			mysql_close($dbh);
		}
		else
		{
		?>
			<h3><center>
			<font color="red"> Fill the field(s) again! </font>
			</h3>
		<?php
		}
	}
?>
<tr>
	<td colspan=2 align=center>
		back to <a href="index.php" class="link">the login page:</a>
	</td>
</tr>
</table>
</html>
